// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.xml;

import java.util.Properties;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.components.ENodeParameterName;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.DatabaseFactory;
import org.dengues.warehouse.i18n.Messages;
import org.dengues.warehouse.viewers.database.NewDatabaseWizardPage;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.wizard.Wizard;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-11-27 qiang.zhang $
 * 
 */
public class NewXmlFileWizard extends Wizard {

    private NewDatabaseWizardPage page;

    private IPath path;

    private DatabaseDiagram database;

    /**
     * qzhang NewProcessWizard constructor comment.
     */
    public NewXmlFileWizard(IPath path) {
        this();
        this.path = path;
        this.database = null;
        setWindowTitle("");
    }

    private NewXmlFileWizard() {
        super();
        setDefaultPageImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.WAREHOUSE_WIZARD_DATABASE));
    }

    public NewXmlFileWizard(DatabaseDiagram database) {
        this();
        this.database = database;
        path = null;
        setWindowTitle(Messages.getString("NewDatabaseWizard.editproperties")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#performFinish()
     */
    @Override
    public boolean performFinish() {
        boolean create = false;
        if (database == null) {
            database = DatabaseFactory.eINSTANCE.createDatabaseDiagram();
            create = true;
        }
        database.setConnId(page.getPropId());
        Properties properties = page.getProperties();
        for (Object str : properties.keySet()) {
            database.getProperties().put(str, properties.get(str));
        }
        database.getProperties().put(ENodeParameterName.DB_COMMENT.getName(), page.getDatabaseComment());
        database.setName(page.getDatabaseName());
        database.setStatus(page.getDatabaseStatus());
        if (create) {
            WarehouseResourceFactory.create(database, path);
        } else {
            WarehouseResourceFactory.save(database);
        }
        return true;
    }

    public DatabaseDiagram getDatabase() {
        return database;
    }

}
